package com.travel.service.impl.admin;

import com.travel.mapper.log.ApiLogMapper;
import com.travel.model.entity.log.ApiLog;
import com.travel.service.Service.AdminDashboardService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//管理员仪表盘服务实现类 获取api统计数据 获取最近api日志
@Service
@RequiredArgsConstructor
public class AdminDashboardServiceImpl implements AdminDashboardService {

    private final ApiLogMapper apiLogMapper;

    @Override
    public Map<String, Integer> getApiStats() {
        try {
            List<Map<String, Object>> statistics = apiLogMapper.getApiStatistics();
            Map<String, Integer> stats = new HashMap<>();
            
            if (statistics != null) {
                for (Map<String, Object> stat : statistics) {
                    String path = (String) stat.get("path");
                    Number count = (Number) stat.get("count");
                    stats.put(path, count.intValue());
                }
            }
            
            return stats;
        } catch (Exception e) {
            e.printStackTrace();
            return new HashMap<>();
        }
    }

    @Override
    public List<ApiLog> getRecentLogs() {
        try {
            List<ApiLog> logs = apiLogMapper.findByRequestTimeBetween(null, null);
            return logs != null ? logs : Collections.emptyList();
        } catch (Exception e) {
            e.printStackTrace();
            return Collections.emptyList();
        }
    }
} 